package cn.tedu.charging.order.amqp;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.core.MessagePropertiesBuilder;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

/**
 * 1.封装rabbitTemplate 不让业务看到rabbitmq的实现
 * 2.业务需要发送什么消息,这里准备发送的方法
 */
@Component
@Slf4j
public class AmqpDelayProducer {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //channel.basicPublish(交换机名称,路由key,消息属性,消息体)
    public void sendDelay(String exchange,String body,Integer delayTime){
        //1.组织消息对象
        //1.1消息体
        byte[] bytes=body.getBytes(StandardCharsets.UTF_8);
        //1.2消息属性 设定延迟时间 expiration
        MessageProperties props=MessagePropertiesBuilder.newInstance()
                .setExpiration(delayTime.toString())
                .setContentType("text/plain")
                .setContentEncoding("UTF-8").build();
        Message message=new Message(bytes,props);
        //2.调用rabbitTemplate将消息发送
        rabbitTemplate.send(exchange,"",message);
    }
}
